import 'reflect-metadata'
import { app, shell, BrowserWindow, ipcMain, globalShortcut } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { initDataBase } from './db/data-source'
import { ipcMainFunc } from './ipMain'

var mainWindow = null
let frame = true
function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 1366,
    height: 736,
    // minHeight: 500,
    // minWidth: 1000,
    // maxHeight: 900,
    // maxWidth: 1600,
    frame: frame,
    resizable: false,
    transparent: true,
    show: false,
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
      contextIsolation: true
    }
  })
  // 注册一个 'Ctrl+F12' 快捷键监听器
  globalShortcut.register('Ctrl+F12', () => {
    mainWindow.webContents.openDevTools()
  })
  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  setTimeout(() => {
    mainWindow.webContents.openDevTools()
  }, 1000)
  console.log('process.versions.modules', process.versions.node)
  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
  mainWindow.webContents.on('mousedown', (event, window, screen) => {
    console.log('鼠标点击位置:', event.x, event.y)
  })

  // 监听键盘点击
  mainWindow.webContents.on('key-down', (event, window, screen) => {
    console.log('键盘按键:', event.key)
  })
}

app.whenReady().then(async () => {
  // if (!AppDataSource.isInitialized) {
  //   const [err, _res] = await to(AppDataSource.initialize())
  //   if (err) {
  //     throw err
  //   }
  // }
  electronApp.setAppUserModelId('com.electron')
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  // IPC test
  ipcMain.on('ping', () => console.log('pong'))

  createWindow()
  initDataBase()

  // 注册通信方法
  ipcMainFunc()
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.

// function toggleFrame(token) {
//   // 如果窗口存在
//   if (mainWindow && !frame && token) {
//     frame = !frame
//     const bounds = mainWindow.getBounds() // 获取窗口的位置和大小
//     const isMaximized = mainWindow.isMaximized() // 是否最大化
//     mainWindow.close() // 关闭当前窗口
//     // 重新创建窗口并应用之前的设置
//     createWindow()
//     mainWindow.setBounds(bounds) // 设置窗口位置和大小
//     if (isMaximized) {
//       mainWindow.maximize() // 重新最大化窗口
//     }
//   }
// }
